package com.proj.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.proj.common.bean.Product;
import com.proj.common.service.IProductService;
import com.proj.common.util.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("product")
public class ProductController {

    @Autowired
    private IProductService productService;

    @PostMapping("/")
    public ResultData insert(@RequestBody Product product) {
        Product data = productService.insert(product);
        return ResultData.success(data);
    }

    @PutMapping("/")
    public ResultData update(@RequestBody Product product) {
        Product data = productService.update(product);
        return ResultData.success(data);
    }
//    @RequestParam接收表单数据（可以有多个@），@RequestBody接收json数据（只能有一个）

    @DeleteMapping("/{id}")
    public ResultData delete(@PathVariable Integer id) {
        int result = productService.delete(id);
        if (result >= 1) {
            return ResultData.success("200","删除成功",null);
        }else{
            return ResultData.fail("删除失败或没找到");
        }
    }

    @GetMapping("/{id}")
    public ResultData getById(@PathVariable Integer id) {
        Product result = productService.getByID(id);
        return ResultData.success(result);
    }

    @GetMapping("/page")
    public ResultData getListByPage(IPage page,Product product) {
        List<Product> data = productService.getListByPage(page,product);
        return ResultData.success(data);
    }

    @GetMapping("/list")
    public ResultData getList(Product product) {
        List<Product> data = productService.getList(product);
        return ResultData.success(data);
    }


}
